﻿{% extends "base.html" %}
{% block title %}Navisworks搜索集生成{% endblock %}

{% block styles %}{{ super() }}
<style>
    .main {
        padding-top: 0px;
    }
</style>
{% endblock %}

{% block scripts %}{{ super() }}
<script src="{{ url_for("static", filename="js/bootstrap-treeview.js") }}"></script>
<script src="{{ url_for("static", filename="js/dropzone.js") }}"></script>
<script>
    tree = [];
    $("#preview").click(function () {
        getTree();
    });
    $("#generate").click(function () {
        $.ajax({
            type: 'post',
            url: '{{ url_for('bp_tool.navisworks_search_sets') }}',
            data: JSON.stringify(tree),
            dataType: 'json',
            contentType: 'application/json',
            success: function (response) {
                var fp = response['filename'];
                if (fp != '') {
                    $("#download").css("display", "inline-block");
                    $("#download").attr("href", "{{ url_for('bp_tool.download') }}" + "?fp=" + fp);


                }
            }
        })

    });
    $("#clear").click(function () {
        $("#textarea").val("");

        $("#generate").css("display", "none");
        $('#tree').treeview();

        $("#download").css("display", "none");
        $("#download").attr("href", "#");
    });

    function getTree() {
        $("#download").css("display", "none");
        $("#download").attr("href", "#");
        tree = [];
        let nodetypes = {
            'area': 'AREA',
            'module': 'Module',
            'packcode': 'SmpPackCode',
            'packboxnumber': 'SmpPackBoxNumber'
        };
        let data = {};
        let errorFlag = false;

        $("#error").empty();
        let textareaText = $("#textarea").val().trim();
        if (textareaText == "") {
            errorFlag = true;
            $("#error").css("display", "block");
            $("#generate").css("display", "none");
            $("#error").append("<p>没有数据</p>");
            $('#tree').treeview();
        } else {
            let textareaData = textareaText.split('\n');
            $.each(textareaData, function (index, element) {
                let e = element.trim().split('\t');
                let i = index + 1;
                if (e.length != 4) {
                    errorFlag = true;
                    $("#error").css("display", "block");
                    $("#generate").css("display", "none");
                    $("#error").append("<p>第 " + i + " 行" + "数据有误:" + element + "</p>");
                    $('#tree').treeview();
                } else {
                    if (nodetypes[e[1]] == undefined) {
                        errorFlag = true;
                        $("#error").css("display", "block");
                        $("#generate").css("display", "none");
                        $("#error").append("<p>第 " + i + " 行" + "非法属性名:" + e[1] + "</p>");
                    } else {
                        if (data[e[0]] == undefined) {
                            data[e[0]] = [{
                                type: nodetypes[e[1]],
                                value: e[2],
                                text: e[3] + "(属性名:" + e[1] + ";属性值:" + e[2] + ")"
                            }]
                        } else {
                            data[e[0]] = data[e[0]].concat([{
                                type: nodetypes[e[1]],
                                value: e[2],
                                text: e[3] + "(属性名:" + e[1] + ";属性值:" + e[2] + ")"
                            }]);
                        }

                    }
                }
            });
            if (!errorFlag) {
                $("#error").css("display", "none");
                $("#generate").css("display", "inline-block");
                $.each(data, function (k, v) {
                    tree = tree.concat([{
                        text: k,
                        nodes: v
                    }]);

                });
                $('#tree').treeview({ data: tree });

            } else {
                $('#tree').treeview();
            }
        }
    }
</script>
{% endblock %}

{% block content %}
<div class="main">
    <div class="container-fluid">
        <div class="row">
            <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">
                <div class="panel panel-primary">
                    <div class="panel-heading">使用说明</div>
                    <div class="panel-body">
                        <p>Navisworks的搜索集功能丰富了我们查看模型的维度。</p>
                        <p>我们可以利用搜索集快速定位拥有个某个特定属性的集和。</p>
                        <p>关于Navisworks搜索集的应用文档, <a target="_blank"
                                href="http://199.234.20.182/mmwiki/subwiki/jmo/#!pdms/navisworks.md">点此前往</a>。
                        </p>

                        <p>这里我们对生成搜索集的数据做一下说明</p>
                        <ul>
                            <li>文件夹名：用于对属性归类</li>
                            <li>属性名：需要搜索的属性的名字，仅限以下四个</li>
                            <ul>
                                <li>area 区域属性</li>
                                <li>module 模块属性</li>
                                <li>packcode 包装编号属性</li>
                                <li>packboxnumber 包装箱号属性</li>
                            </ul>
                            <li>属性值：需要搜索的属性的值。</li>
                            <li>显示名：用于在搜索集目录树上显示的名称。</li>
                        </ul>
                        <p>以下是数据示例:请在Excel中编辑好之后粘贴到文本框中。(不用复制标题)</p>
                    </div>
                    <table class="table table-bordered table-striped">
                        <tr>
                            <th>文件夹名</th>
                            <th>属性名</th>
                            <th>属性值</th>
                            <th>显示名</th>
                        </tr>
                        <tr>
                            <td>模块</td>
                            <td>area</td>
                            <td>A0</td>
                            <td>A0</td>
                        </tr>
                        <tr>
                            <td>模块</td>
                            <td>module</td>
                            <td>101</td>
                            <td>101</td>
                        </tr>
                        <tr>
                            <td>模块</td>
                            <td>module</td>
                            <td>102</td>
                            <td>102</td>
                        </tr>
                        <tr>
                            <td>模块</td>
                            <td>module</td>
                            <td>103</td>
                            <td>103</td>
                        </tr>
                        <tr>
                            <td>模块</td>
                            <td>module</td>
                            <td>104</td>
                            <td>104</td>
                        </tr>
                        <tr>
                            <td>散件</td>
                            <td>packcode</td>
                            <td>Pipe001</td>
                            <td>Pipe001</td>
                        </tr>
                        <tr>
                            <td>散件</td>
                            <td>packcode</td>
                            <td>Stru001</td>
                            <td>Stru001</td>
                        </tr>
                        <tr>
                            <td>散件</td>
                            <td>packcode</td>
                            <td>Elet001</td>
                            <td>Elet001</td>
                        </tr>
                        <tr>
                            <td>散件</td>
                            <td>packboxnumber</td>
                            <td>001</td>
                            <td>001</td>
                        </tr>
                        <tr>
                            <td>散件</td>
                            <td>packboxnumber</td>
                            <td>002</td>
                            <td>002</td>
                        </tr>
                    </table>
                </div>
            </div>


            <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6">

                <div id="preview" class="btn btn-success" style="margin-bottom: 5px;">预览</div>
                <div id="clear" class="btn btn-warning" style="margin-bottom: 5px;">清空</div>
                <div id="generate" class="btn btn-info" style="margin-bottom:5px;display : none;">生成</div>
                <a id="download" href="#" style="display : none;">下载</a>
                <textarea id="textarea" class="form-control" rows="15">模块	area	A0	A0
模块	module	101	101
模块	module	102	102
模块	module	103	103
模块	module	104	104
散件	packcode	Pipe001	Pipe001
散件	packcode	Stru001	Stru001
散件	packcode	Elet001	Elet001
散件	packboxnumber	001	001
散件	packboxnumber	002	002</textarea>
                <div id="error" class="alert alert-danger" style="margin-top: 10px; display: none">
                </div>
                <div id="tree" style="margin-top: 10px"></div>
            </div>


        </div>

    </div>
</div>
{% endblock %}